import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { useUserStore } from './user'
import { insertCartAPI, findNewCartListAPI, delCartAPI, mergeCartAPI } from '@/apis/cart'

export const useCartStore = defineStore('cart', () => {
    const cartList = ref([])
    const selectedCartList = computed(() => cartList.value.filter(item => item.selected == true))
    //登陆状态相关信息
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)

    const updateNewList = async () => {
        const res = await findNewCartListAPI() //2.向服务端获取商品列表
        cartList.value = res.result //3.覆盖本地商品列表
    }
    const addCart = async (goods) => {
        //判断是否登录
        if (isLogin.value) {
            await insertCartAPI({//1.向服务端插入新商品
                skuId: goods.skuId,
                count: goods.count
            })
            await updateNewList()
        } else {
            //判断是否已经添加过了
            // 思路：通过匹配传递过来的商品对象中的skuId能不能在cartList中找到，找到了就是添加过
            const item = cartList.value.find((item) => goods.skuId === item.skuId)
            if (item) {
                // 找到了
                item.count += goods.count
            } else {
                // 没找到
                cartList.value.push(goods)
            }
        }
    }
    const deleteCart = async (skuId) => {
        //根据skuID删除
        if (isLogin.value) {
            await delCartAPI([skuId])
            await updateNewList()
        } else {
            const index = cartList.value.findIndex((item) => item.skuId == skuId)
            cartList.value.splice(index, 1)
        }

    }


    // 计算属性
    // 1. 总的数量 所有项的count之和
    const addCount = computed(() => cartList.value.reduce((acc, cur) => acc + cur.count, 0))

    // 2. 总价 所有项的count*price之和
    const sumPrice = computed(() => cartList.value.reduce((acc, cur) => acc + cur.count * cur.price, 0))

    //单选框状态改变
    // 单选功能
    const singleCheck = (skuId, selected) => {
        // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected
        const item = cartList.value.find((item) => item.skuId === skuId)
        item.selected = selected
    }
    //全选框，针对全选框，同样使用退化的v-model
    // 全选功能action
    const allCheck = (selected) => {
        cartList.value.forEach(item => item.selected = selected)
    }

    // 是否全选计算属性
    const isAll = computed(() => cartList.value.every((item) => item.selected))

    // 3. 选择的数量 选择项的count之和
    const selectedAddCount = computed(() => selectedCartList.value.reduce((acc, cur) => acc + cur.count, 0))

    // 4. 选择的总价 选择项的count*price之和
    const selectedSumPrice = computed(() => selectedCartList.value.reduce((acc, cur) => acc + cur.count * cur.price, 0))


    //
    const clearCart = () => {
        cartList.value = []
    }
    return {
        cartList,
        addCart,
        deleteCart,
        addCount,
        sumPrice,
        singleCheck,
        allCheck,
        isAll,
        selectedAddCount,
        selectedSumPrice,
        clearCart,
        updateNewList
    }

}, {
    persist: true
}
)